package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetApplyInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Asset apply fragment.
 *
 * @author sHadowLess
 */
public class AssetApplyFragment extends BaseFragment {

    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_ReceiptNum)
    TextView assetApplyReceiptNum;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_MakeTime)
    TextView assetApplyMakeTime;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_Maker)
    TextView assetApplyMaker;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_department)
    TextView assetApplyDepartment;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_applier)
    TextView assetApplyApplier;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_applyExplain)
    TextView assetApplyApplyExplain;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_supplier)
    TextView assetApplySupplier;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_procurement)
    TextView assetApplyProcurement;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_totalAmount)
    TextView assetApplyTotalAmount;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_isOutPay)
    TextView assetApplyIsOutPay;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_inquiry)
    TextView assetApplyInquiry;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_payExplain)
    TextView assetApplyPayExplain;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_SingleReason)
    TextView assetApplySingleReason;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_SingleAmount)
    TextView assetApplySingleAmount;
    /**
     * The Cost single single amount.
     */
    @BindView(R.id.asset_apply_ExpectedDate)
    TextView assetApplyExpectedDate;
    /**
     * The Asset apply check process.
     */
    @BindView(R.id.asset_apply_check_Process)
    LinearLayout assetApplyCheckProcess;
    /**
     * The Cost temp item.
     */
    @BindView(R.id.asset_apply_item)
    LinearLayout assetApplyItem;
    /**
     * The Apply tab.
     */
    @BindView(R.id.asset_apply_info_item)
    LinearLayout assetApplyInfoItem;

    private final String url = "api/FixedAppForm/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset_apply_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            String billCode = bundle.getString("billCode");
            initAssetApplyInfo(orderId, userName, billCode);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, assetApplyCheckProcess);
        }
    }

    @Override
    protected void initView() {

    }

    private void initAssetApplyInfo(String orderId, String userName, String billCode) {
        OrderPresenter
                .getSingleton()
                .getAssetApplyInfo(orderId, userName, new RequestCallBack<AssetApplyInfo>() {
                    @Override
                    public void success(RequestResult<AssetApplyInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                AssetApplyInfo.DataDTO dataBean = result.getResult().getData();
                                List<AssetApplyInfo.DataDTO.ItemListDTO> itemList = dataBean.getItemList();
                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                String departmentName = dataBean.getDepartmentName();
                                String applicantName = dataBean.getApplicantName();
                                String reason = dataBean.getReason();
                                String supplierName = dataBean.getSupplierName();
                                String confirmUserName = dataBean.getConfirmUserName();
                                String totalCostStr = dataBean.getTotalCostStr();
                                boolean isTemp = dataBean.isTemp();
                                String enquiryInfo = dataBean.getEnquiryInfo();
                                String remark = dataBean.getRemark();
                                assetApplyReceiptNum.setText(id);
                                assetApplyMakeTime.setText(createTimeStr);
                                assetApplyMaker.setText(createUserName);
                                assetApplyDepartment.setText(departmentName);
                                assetApplyApplier.setText(applicantName);
                                assetApplyApplyExplain.setText(reason);
                                assetApplySupplier.setText(supplierName);
                                assetApplyProcurement.setText(confirmUserName);
                                assetApplyTotalAmount.setText(totalCostStr);
                                if (isTemp) {
                                    assetApplyIsOutPay.setText("√ 暂支");
                                    assetApplyItem.setVisibility(View.VISIBLE);
                                    String tempReason = dataBean.getTempReason();
                                    String tempCost = getDecimal(dataBean.getTempCost().doubleValue());
                                    String returnDateStr = dataBean.getReturnDateStr();
                                    assetApplySingleReason.setText(tempReason);
                                    assetApplySingleAmount.setText(tempCost);
                                    assetApplyExpectedDate.setText(returnDateStr);
                                } else {
                                    assetApplyItem.setVisibility(View.GONE);
                                }
                                assetApplyInquiry.setText(enquiryInfo);
                                assetApplyPayExplain.setText(remark);


                                for (AssetApplyInfo.DataDTO.ItemListDTO itemListDTO : itemList) {
                                    View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_asset_apply_item, null, false);
                                    LinearLayout mainTab = view.findViewById(R.id.apply_item);
                                    TextView assetApplyAssetId = view.findViewById(R.id.asset_apply_assetId);
                                    TextView assetApplyAssetName = view.findViewById(R.id.asset_apply_assetName);
                                    TextView assetApplyModel = view.findViewById(R.id.asset_apply_model);
                                    TextView assetApplySize = view.findViewById(R.id.asset_apply_size);
                                    TextView assetApplyNum = view.findViewById(R.id.asset_apply_num);
                                    TextView assetApplyUnitPrice = view.findViewById(R.id.asset_apply_unitPrice);
                                    TextView assetApplyTotalAmount = view.findViewById(R.id.asset_apply_totalAmount);
                                    TextView assetApplyRemark = view.findViewById(R.id.asset_apply_remark);

                                    String fixedTypeName = itemListDTO.getFixedTypeName();
                                    String assetName = itemListDTO.getAssetName();
                                    String modelNumber = itemListDTO.getModelNumber();
                                    String size = itemListDTO.getSize();
                                    String num = getDecimal(itemListDTO.getNum().doubleValue());
                                    String price = getDecimal(itemListDTO.getPrice().doubleValue());
                                    String totalPrice = getDecimal(itemListDTO.getTotalPrice().doubleValue());
                                    String remarkInfo = itemListDTO.getRemark();

                                    assetApplyAssetId.setText(fixedTypeName);
                                    assetApplyAssetName.setText(assetName);
                                    assetApplyModel.setText(modelNumber);
                                    assetApplySize.setText(size);
                                    assetApplyNum.setText(num);
                                    assetApplyUnitPrice.setText(price);
                                    assetApplyTotalAmount.setText(totalPrice);
                                    assetApplyRemark.setText(remarkInfo);
                                    assetApplyInfoItem.addView(mainTab);
                                }

                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
